$(function () {
  //var API_URL= window.location.host+window.location.port;
  //var asd=$('#prefix').attr("href")
  //console.log(asd);
  //window.hostPrefix=$('#prefix').attr("href");
  
var objslide;
 
    //var is_slide_on = false;
var pathtools = {
    ajax_logout: function(){
        var toast;
        $.ajax({
               type: "POST",
               dataType: 'jsonp',
               jsonp: '_c',
               url: window.hostPrefix+"logout",
               data: "",
               beforeSend: function () {toast=layer.open({
                                type: 2
                                ,content: '退出中..'
                                ,time:2
                              });
                            },
               success: function(result){
                //eval("var data="+data);
                //result = (new Function("","return "+result))(); 
                if (result.status_code==0) {
                  layer.close(toast);
                  layer.open({
                    content: result.info
                    ,skin: 'msg'
                    ,time: 2
                    });

                  location.href="#/login"
                    console.log(result.info);
                //    var index = layer.open({
                //     content: result.info
                //     ,btn: '确定'
                //     ,yes: function(index){
                //   layer.close(index);
                  
                //   location.href="#/login"
                // }
                //  });
                  } else{
                    
                    // console.log(result.info);
                    // layer.open({
                    // content: result.info
                    // ,skin: 'msg'
                    // ,time: 2
                    // });
                    //layer.close(toast);
                    //window.location.href=result.data.url
                        //layer.close(index);
              };
                        
                  },
                error: function(errorMsg) {
                        
                    layer.open({
                    content: "信息获取失败"
                    ,skin: 'msg'
                    ,time: 2
                    });
                        console.log(errorMsg.responseText);
                    }
            });

    },
    ajax_login: function (logindata) {

       var toast;
        $.ajax({
               type: "POST",
               dataType: 'jsonp',
               jsonp: '_c',
               url: window.hostPrefix+"login",
               data: logindata,
               beforeSend: function () {toast=layer.open({
                                type: 2
                                ,content: '登录中..'
                                ,time:2
                              });
                            },
               success: function(result){
                //eval("var data="+data);
                //result = (new Function("","return "+result))(); 
                if (result.status_code==0) {
                    console.log(result.info);
                   var index = layer.open({
                    content: result.info
                    ,btn: '确定'
                    ,yes: function(index){
                  layer.close(index);
                  //layer.close(toast);
                  //return false;
                }
                 });
                  } else{
                    
                    console.log(result.info);
                    layer.open({
                    content: result.info
                    ,skin: 'msg'
                    ,time: 2
                    });
                    layer.close(toast);
                    location.href="#/my"
                        //layer.close(index);
                        //return true;
              };
                        
                  },
                error: function(errorMsg) {
                        console.log(errorMsg.responseText);
                    }
            });
         
         
      // body...
    },
    captchaButtonDisableTimer: function(btn, timer) {
       //opt.captchaButtonDisableTimer($('a#reset_pass_getcaptcha.weui_btn'),60);
                if (timer > 0) {
                    btn.text(timer + "s后可重新发送");//此处更改了文字
                    setTimeout(function() {opt.captchaButtonDisableTimer(btn, timer - 1); }, 1000);
                    btn.addClass('weui_btn_disabled').addClass('weui_btn_default').removeClass('weui_btn_primary').attr('data-zt',"1");
                }
                else {
                    btn.text("获取短信验证码");
                    btn.removeClass('weui_btn_disabled').removeClass('weui_btn_default').addClass('weui_btn_primary').attr('data-zt',"0");
                }
            },
    getQueryString:function (name) { 
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
      var r = window.location.search.substr(1).match(reg); 
      if (r != null) return unescape(r[2]); return null; 
      }, 
    slider_start:function(){
      clearInterval(objslide);
      imgLen = $("#slider-box ul li").length;
      var curIndex=0;
       // console.log(imgLen);
      objslide = setInterval(function(){
       //console.log("当前索引:"+curIndex);
        if(curIndex < imgLen-1){ 
            curIndex ++; 
          }else{ 
            curIndex = 0;
          }
  //调用变换处理函数
  pathtools.changeTo(curIndex); 
        },3000);
      },
    	menubar_refresh:function(){
    		
    		var hash;
    		if (hash == "/client" ){
    			//alert(location.hash);
    			$('#menubar').find(str).click();
    		}	
    	},
    onslide:function(){
            $('#slider-box').touchSlider({
                    box: '#slider-box',
                    arrow: false,
                    auto: true,
                    autoTime: 3000
                        });

    },
    changeTo:function (num){ 
        //console.log("执行目标函数");
        $("#slider").find("li").eq(num).addClass('active').siblings('.active').removeClass('active');
      }

    }
	//定义路由器pathjs
	//首先Path.map("#/跳转路径名").to(function()){执行目标函数}
	//例如http://127.0.0.1/#/index
	//然后Path.root("#/跳转路径名")定义默认访问路径,在不指定路径时,默认访问的路径
	//例如设置path.root("#/about")访问http://127.0.0.1/时,会自动跳转到http://127.0.0.1/#/about
	//最后设置开始监听Path.listen()
	Path.map("#/index").to(function(){
        //alert("跳转主页index")
        var data = {
        	test:"测试变量index"

        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_index').html(),data));
        pathtools.slider_start();
        $('#index_con').on('click', '.list li', function () {
        	
            //$(this).addClass('active').siblings('.active').removeClass('active');
            var li_href=$(this).attr("href");
    		location.href=li_href;
    		//alert(li_href);
        })
       
    });
    Path.map("#/client").to(function(){
        alert(location.hash)
        //console.log($('#topbar_title').text('12312'));
        var data = {
        	test:"测试变量client"

        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_client').html(),data));
               //模板数据
    var data = {
      name: 'Fxtpl模板引擎',
      list: [
        "目录1",
        "目录2",
        "目录3",
        "目录4",
        "目录5"
      ]
    };
    //渲染模板
    Fxtpl.render('myTemplate', data);
        });
    Path.map("#/login").to(function(){
        //alert(location.hash)
        var data = {
          test:"测试变量login"

        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_login').html(),data));
          $('#container').on('click','#login_submit',function () {
            var logindata={username:$("input[name=username]").val(),password:$("input[name=password]").val()};
            //{username:"admin",password:"123456"}
            if ($("input[name=username]").val()=="") {
              var u_notnull=layer.open({
                          content: '用户名不能为空,请检查重试!'
                          ,btn: '确定'
                          ,yes: function(index){
                        //location.herf="#/wx_login";
                       // alert(0);
                        //window.location.href="#/wx_login"
                        layer.close(u_notnull);
                        
                      }
                        });
              //console.log($("input[name=username]").val());
              //alert(0);
            } else {
              console.log(logindata);
            pathtools.ajax_login(logindata)


            }
            
       //username:$("#L_username").attr("value"),password:$("#L_password").attr("value")
        });
        });
    Path.map("#/wx_login").to(function(){
        //alert(location.hash)
        var data = {
            test:"测试变量wx_login",
            info:"正在跳转微信授权登录页"
        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_wx_login').html(),data));
        
        setTimeout(function() {window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx32ff322faf0ccbbd&redirect_uri=http://wc.1688368.com/tj_ui/index.html&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"},2000)
        
        });
    Path.map("#/discover").to(function(){
        //alert("跳转主页discover")

        var data = {
        	test:"测试变量discover"


        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_discover').html(),data));
       location.herf="#/wx_login";
       var index = layer.open({
        content: '移动版和PC版不能同时存在同一页面4'
        ,btn: '我知道了'
        ,yes: function(index){
      //location.herf="#/wx_login";
     // alert(0);
      window.location.href="#/wx_login"
      layer.close(index);
      
    }
      });
    });
  
    Path.map("#/bbs").to(function(){
        //alert("跳转主页bbs")
        var data = {
        	test:"测试变量bbs"

        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_bbs').html(),data));
        $.ajax({
               type: "POST",
               dataType: 'jsonp',
               jsonp: '_c',
               url: window.hostPrefix+"get_user_info",
               data: "",
               success: function(result){
                
                if (result.status_code==1) {
                    console.log(result);
                   var index = layer.open({
                    content: result.info
                    ,btn: '确定'
                    ,yes: function(index){
                  layer.close(index);

                }
                 });
                  } else{
                    
                    console.log(result.info);
                    layer.open({
                    content: result.info
                    ,skin: 'msg'
                    ,time: 2
                    });
                    
              };
                        
                  },
                error: function(errorMsg) {
                        //console.log(errorMsg.responseText);
                    }
            });
    });
      Path.map("#/my").to(function(){

        //alert("跳转主页my")
        var data = {
        	test:"测试变量my"

        };
        $('#topbar_title').text(data.test)
        $('#container').html(Fxtpl.compile($('#tpl_my').html(),data));
        //alert(0);
        $.ajax({
               async: false,
               type: "POST",
               dataType: 'jsonp',
               jsonp: '_c',
               url: window.hostPrefix+"login_check",
               data: "",
               success: function(result){
                //alert(result.status_code);
                if (result.status_code==0) {
                    console.log(result.status_code);
                   var index = layer.open({
                    content: result.info
                    ,btn: '确定'
                    ,yes: function(index){
                      location.href="#/login";
                  layer.close(index);

                }
                 });
                  } else{
                    //alert(result.status_code);
                    console.log(result.status_code);
                    layer.open({
                    content: result.info
                    ,skin: 'msg'
                    ,time: 2
                    });
                    
              };
                        
                  },
                error: function(errorMsg) {
                  //alert("errorMsg"+errorMsg.responseText);
                        //console.log(errorMsg.responseText);
                    }
            });
       $('#container').on('click','#test',function () {

        pathtools.ajax_login({username:"admin",password:"123456"})
         
         })
        $('#container').on('click','#test2',function () {
                    pathtools.ajax_logout();
                 
                 })
            });
    
    Path.root("#/index");
    Path.listen(); 
    
    //主bar单击事件绑定
        $('#menubar').on('click', '.tab', function () {
            $(this).addClass('active').siblings('.active').removeClass('active');
            var li_href=$(this).attr("href");
    		    location.href=li_href;
    		//alert(li_href);
        })
//      .on('click','#menubar_index',function () {
//      	location.href="#/index"
//                 // opt_ajax.setting();
//          })
//      .on('click','#menubar_client',function () {
//      	location.href="#/client"
//                 // opt_ajax.setting();
//          })
//      .on('click','#menubar_discover',function () {
//      	location.href="#/discover"
//                 // opt_ajax.setting();
//          })
//      .on('click','#menubar_bbs',function () {
//      	location.href="#/bbs"
//                 // opt_ajax.setting();
//          })
//      .on('click','#menubar_my',function () {
//      	location.href="#/my"
//                 // opt_ajax.setting();
//          })
        
        
})